package cn.csc.module.product.controller.admin.attrjson;

import cn.csc.framework.common.pojo.CommonResult;
import cn.csc.framework.common.pojo.PageResult;
import cn.csc.framework.excel.core.util.ExcelUtils;
import cn.csc.framework.operatelog.core.annotations.OperateLog;
import cn.csc.module.product.controller.admin.attrjson.vo.*;
import cn.csc.module.product.convert.attrjson.AttrJsonConvert;
import cn.csc.module.product.dal.dataobject.attrjson.AttrJsonDO;
import cn.csc.module.product.service.attrjson.AttrJsonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Collection;
import java.util.List;

import static cn.csc.framework.common.pojo.CommonResult.success;
import static cn.csc.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - spu属性规格参数值信息")
@RestController
@RequestMapping("/spu/attr-json")
@Validated
public class AttrJsonController {

    @Resource
    private AttrJsonService attrJsonService;

    @PostMapping("/create")
    @ApiOperation("创建spu属性规格参数值信息")
    @PreAuthorize("@ss.hasPermission('spu:attr-json:create')")
    public CommonResult<BigInteger> createAttrJson(@Valid @RequestBody AttrJsonCreateReqVO createReqVO) {
        return success(attrJsonService.createAttrJson(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新spu属性规格参数值信息")
    @PreAuthorize("@ss.hasPermission('spu:attr-json:update')")
    public CommonResult<Boolean> updateAttrJson(@Valid @RequestBody AttrJsonUpdateReqVO updateReqVO) {
        attrJsonService.updateAttrJson(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除spu属性规格参数值信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('spu:attr-json:delete')")
    public CommonResult<Boolean> deleteAttrJson(@RequestParam("id") BigInteger id) {
        attrJsonService.deleteAttrJson(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得spu属性规格参数值信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('spu:attr-json:query')")
    public CommonResult<AttrJsonRespVO> getAttrJson(@RequestParam("id") BigInteger id) {
        AttrJsonDO attrJson = attrJsonService.getAttrJson(id);
        return success(AttrJsonConvert.INSTANCE.convert(attrJson));
    }

    @GetMapping("/list")
    @ApiOperation("获得spu属性规格参数值信息列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('spu:attr-json:query')")
    public CommonResult<List<AttrJsonRespVO>> getAttrJsonList(@RequestParam("ids") Collection<BigInteger> ids) {
        List<AttrJsonDO> list = attrJsonService.getAttrJsonList(ids);
        return success(AttrJsonConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得spu属性规格参数值信息分页")
    @PreAuthorize("@ss.hasPermission('spu:attr-json:query')")
    public CommonResult<PageResult<AttrJsonRespVO>> getAttrJsonPage(@Valid AttrJsonPageReqVO pageVO) {
        PageResult<AttrJsonDO> pageResult = attrJsonService.getAttrJsonPage(pageVO);
        return success(AttrJsonConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出spu属性规格参数值信息 Excel")
    @PreAuthorize("@ss.hasPermission('spu:attr-json:export')")
    @OperateLog(type = EXPORT)
    public void exportAttrJsonExcel(@Valid AttrJsonExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<AttrJsonDO> list = attrJsonService.getAttrJsonList(exportReqVO);
        // 导出 Excel
        List<AttrJsonExcelVO> datas = AttrJsonConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "spu属性规格参数值信息.xls", "数据", AttrJsonExcelVO.class, datas);
    }

}
